package cn.edu.zjicm.wordsnet_d.activity;

import java.util.Timer;
import java.util.TimerTask;

import com.umeng.analytics.MobclickAgent;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.base.WordBaseActivity;
import cn.edu.zjicm.wordsnet_d.synchronize.ForgetPwdInterface;
import cn.edu.zjicm.wordsnet_d.synchronize.UserInterface;

public class ConfirmCodeActivity extends WordBaseActivity implements OnClickListener {
	TextView email;
	EditText confirmCode;
	Button commitCodeBtn, repetGetCodeBtn;
	ProgressDialog dialog;
	Timer timer;
	MyTimerTask task;
	String loginId;
	int time = 30;
	
	Handler confirmHandler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what != ForgetPwdInterface.MSG_START) {
				if (dialog != null) {
					dialog.dismiss();
					dialog = null;
				}
			}
			switch (msg.what) {
			case ForgetPwdInterface.MSG_START:
				dialog = ProgressDialog.show(ConfirmCodeActivity.this, null, "正在验证...");
				break;			
			case ForgetPwdInterface.MSG_SUCCESS:
				Toast.makeText(ConfirmCodeActivity.this, "邮件已重新发送，请注意查收", Toast.LENGTH_SHORT).show();
				confirmCode.setText("");
				commitCodeBtn.setEnabled(false);
				timer = new Timer();
				time = 30;
				task = new MyTimerTask();
				timer.schedule(task, 1000, 1000);
				break;
			case ForgetPwdInterface.MSG_UNSUCCESS:
				Toast.makeText(ConfirmCodeActivity.this, "验证失败，请稍后重试", Toast.LENGTH_SHORT).show();
				break;
			case ForgetPwdInterface.MSG_BAD_NETWORK:
				Toast.makeText(ConfirmCodeActivity.this, UserInterface.MSG_BAD_NETWORK_STRING, Toast.LENGTH_SHORT).show();
				break;
			case ForgetPwdInterface.MSG_WEB_SITE_ACCESS_FAILURE:
				Toast.makeText(ConfirmCodeActivity.this, UserInterface.MSG_WEB_SITE_ACCESS_FAILURE_STRING, Toast.LENGTH_SHORT).show();
				break;
			case ForgetPwdInterface.MSG_REPEAT_TIMER:
				repetGetCodeBtn.setText(time+"秒后重发");
				time--;
				if(time<=0){
					repetGetCodeBtn.setText("重新发送");
					timer.cancel();
					timer = null;
					task = null;
					repetGetCodeBtn.setEnabled(true);
				}
				break;
			case ForgetPwdInterface.MSG_SUCCESS_TO_CHANGE:
				Intent in = new Intent(ConfirmCodeActivity.this, ChangePwdWithCodeActivity.class);
				in.putExtra("loginId", loginId);
				in.putExtra("code", confirmCode.getText().toString());
				startActivity(in);
				break;
			case ForgetPwdInterface.MSG_ERROR_CODE:
				Toast.makeText(ConfirmCodeActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTitleBarStyle(TITLE_BAR_STYLE_BACK_BUTTON);
		setTitleText("忘记密码");
		setContentView(R.layout.activity_confirm_code);
		email = (TextView) findViewById(R.id.confirm_email);
		confirmCode = (EditText) findViewById(R.id.confirm_code);
		commitCodeBtn = (Button) findViewById(R.id.confirm_commit);
		repetGetCodeBtn = (Button) findViewById(R.id.confirm_repetGet);
		loginId = getIntent().getStringExtra("email");
		email.setText(loginId);
		confirmCode.addTextChangedListener(textWatcher);
		commitCodeBtn.setOnClickListener(this);
		repetGetCodeBtn.setOnClickListener(this);
		task = new MyTimerTask();
		timer = new Timer();
		timer.schedule(task, 1000, 1000);
	}
	class MyTimerTask extends TimerTask{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			confirmHandler.sendMessage(confirmHandler.obtainMessage(ForgetPwdInterface.MSG_REPEAT_TIMER));
		}
		
	}

	TextWatcher textWatcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			// TODO Auto-generated method stub
			if (confirmCode.getText().length() > 0) {
				commitCodeBtn.setEnabled(true);
			} else {
				commitCodeBtn.setEnabled(false);
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			// TODO Auto-generated method stub

		}

		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub

		}
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.confirm_commit:
			toConfirm();
			break;

		case R.id.confirm_repetGet:
			ForgetPwdInterface.getInstance(ConfirmCodeActivity.this).toSendEmail(loginId, confirmHandler);
			repetGetCodeBtn.setEnabled(false);
			break;
		}
	}

	private void toConfirm() {
		// TODO Auto-generated method stub
		String code = confirmCode.getText().toString();
		confirmHandler.sendMessage(confirmHandler.obtainMessage(ForgetPwdInterface.MSG_START));
		ForgetPwdInterface.getInstance(ConfirmCodeActivity.this).toConfirmCode(loginId, code, confirmHandler);

	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}
}
